package com.example.mapper;

import com.example.domain.entity.News;
import com.example.domain.entity.Star;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zeer
 * @since 2023-04-30
 */
@Mapper
public interface StarMapper extends BaseMapper<Star> {
    @Select("SELECT n.newsTag, ROUND(AVG(s.collect_rate),4) AS avg_collect_rate\n" +
            "FROM (\n" +
            "    SELECT news.id, news.newsTag, news.click, COUNT(star.newsId) AS collect_num, COUNT(star.newsId)/news.click AS collect_rate\n" +
            "    FROM news\n" +
            "    LEFT JOIN star ON news.id = star.newsId\n" +
            "    GROUP BY star.newsId\n" +
            ") AS s\n" +
            "JOIN news n ON s.id = n.id\n" +
            "GROUP BY n.newsTag")
    List<Map<String,Object>> selectByStar();
}
